// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/ScheduleAttendanceApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/base/model/SchedulePlanListQueryReq'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryScheduleAttendanceRecordList', DTO)
    return [resp.comQuery.totalCount, resp.content || [], resp.timeEnum || []]
  },
  dealList([arr], { state: { MAP } }) {
    if (MAP.DAY_LIST) MAP.DAY_LIST = arr
  },
  transList(arr, { state: { MAP: { DAY_LIST }, DICT: { BEFORE_STATUS, AFTER_STATUS } } }) {
    arr.forEach((item) => {
      const { map = {}, phone } = item
      item.userPhone = phone

      DAY_LIST.forEach(({ day }) => {
        if (map[day]) {
          // const { record = {}, timeName, timeBegin, timeEnd } = map[day]
          const { record = {} } = map[day]
          const trans = (key) => record[key] ? CORE.timeStr(record[key]).slice(11) : ''
          const [begin, end] = [trans('workHoursValid'), trans('workAfterHoursValid')]
          const before = record.beforeStatus !== 1 ? CORE.dict(BEFORE_STATUS, record.beforeStatus) : ''
          const after = record.afterStatus !== 1 ? CORE.dict(AFTER_STATUS, record.afterStatus) : ''
          item[day] = [
            // { id: 1, name: `${timeName} ${timeBegin}-${timeEnd}` },
            { id: 2, name: `上：${begin}${before}`, time: begin, status: record.workHoursStatus, statusName: before },
            { id: 3, name: `下：${end}${after}`, time: end, status: record.workAfterHoursStatus, statusName: after },
          ]
        }
      })
    })
    return arr
  },
}
